package com.atguigu.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.atguigu.commentutils.R;
import com.atguigu.vod.Utils.ConstandVodUtils;
import com.atguigu.vod.Utils.InitVodCilent;
import com.atguigu.vod.service.VodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@Api(description = "上传视频")
@RequestMapping("/eduvod/video")
@CrossOrigin
public class VodController {
    //注入servic
    @Autowired
    private VodService vodService;

    //上传视频到阿里云
    @PostMapping("uploadAlyiVideo")
    public R uploadAlyiVideo(MultipartFile file){
        String video = vodService.uploadVideoAly(file);
        return R.ok().data("videoId",video);
    }

    //根据视频ID删除阿里云视频
    @DeleteMapping("removeVideo/{id}")
    public R removeVideo(@PathVariable String id){
        try {
            //初始化对象
            DefaultAcsClient client = InitVodCilent.initVodClient(ConstandVodUtils.ACCESS_KEY_ID,ConstandVodUtils.ACCESS_KEY_SECRET);
            //创建删除视频request
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置视频Id
            request.setVideoIds(id);
            client.getAcsResponse(request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error().data("result","删除失败");
        }
    }

    //删除多个视频
    @DeleteMapping("delete-batch")
    @ApiOperation(value = "删除多个视频")
    public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList){
        vodService.removeMoreAlyVideo(videoIdList);
        return R.ok();
    }
}
